/**
 * Created by yangjiao on 2020/3/19.
 */
function getAfterDate (num) {
  let date = new Date()
  date.setHours(9)
  date.setMinutes(0)
  date.setSeconds(0)
  date.setMilliseconds(0)
  date.setDate(date.getDate() + num)
  return date
}
export default {
  data () {
    return {
      formStacked: {
        contactContent: '',
        region: '',
        executor: ''
      },
      agentOptions: [],
      rules: {
        contactContent: [
          {required: true, message: this.$t('customer.contactPlan.pleaseEnterContactPlanContent'), trigger: 'blur'}
        ],
        region: [
          {required: true, type: 'date', message: this.$t('customer.contactPlan.pleasePickTime'), trigger: 'change'}
        ]
      },
      pickerOptions: {
        disabledDate (time) {
          return time.getTime() + 86400000 < Date.now()
        },
        shortcuts: [{
          text: this.$t('public.afterHalfHour'),
          onClick (picker) {
            let date = new Date()
            date.setMinutes(date.getMinutes() + 30)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.afterOneHour'),
          onClick (picker) {
            let date = new Date()
            date.setHours(date.getHours() + 1)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.tomorrow'),
          onClick (picker) {
            let date = getAfterDate(1)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.afterTomorrow'),
          onClick (picker) {
            let date = getAfterDate(2)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.after3Days'),
          onClick (picker) {
            let date = getAfterDate(3)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.after14Days'),
          onClick (picker) {
            let date = getAfterDate(14)
            picker.$emit('pick', date)
          }
        }, {
          text: this.$t('public.after30Days'),
          onClick (picker) {
            let date = getAfterDate(30)
            picker.$emit('pick', date)
          }
        }]
      }
    }
  },
  props: {
    editType: String,
    initForm: {
      type: Object,
      defalut: () => {
        return {
          contactContent: '',
          region: '',
          executor: ''
        }
      }
    }
  },
  watch: {
    initForm: {
      immediate: true,
      handler: function (cur, old) {
        let that = this
        if (this.editType !== 'add') {
          that.formStacked = cur
        } else {
          that.formStacked = {
            contactContent: '',
            region: '',
            executor: ''
          }
        }
        if (!that.formStacked.executor) {
          this.formStacked.executor = ''
        }
        this.$nextTick(() => {
          this.$refs.formStacked.clearValidate()
        })
      }
    }
  },
  computed: {
//      form () {
//        return this.formStacked
//      }
  },
  methods: {
    submitForm () {
      this.$refs.formStacked.validate((valid) => {
        if (valid) {
          this.$emit('submit', this.formStacked)
//            this.$refs.formStacked.resetFields()
        }
      })
    },
    update () {
      this.$refs.formStacked.validate((valid) => {
        if (valid) {
          this.$emit('update', this.formStacked)
        }
      })
    }
  },
  beforeMount () {
//      this.$set(this, 'formStacked', this.initForm)
    this.agentOptions = this.$getCache('agents').map((item) => {
      return {value: item._id, label: `${item.displayName}[${item.exten}]`}
    })
    this.agentOptions.unshift({value: '', label: '请选择'})
  }
}
